<%@page import="com.liferay.portal.model.ModelHintsConstants"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@include file="/html/portlet/chart/admin/init.jsp"%>


<%

	String randomNamespace = PortalUtil.generateRandomKey(request, "taglib_ui_input_asset_links_page") + StringPool.UNDERLINE;

	String eventName = randomNamespace + "selectDscfg";

	Option option = null;
	DataSourceCfg dscfg = null;
	long optionId = ParamUtil.getLong(request, "optionId");
	try {
		if (optionId > 0) {
			option = OptionLocalServiceUtil.getOption(optionId);
			if(option!=null){
				long dataSourceId = option.getDataSourceId();
				if(dataSourceId>0){
					dscfg = DataSourceCfgLocalServiceUtil.getDataSourceCfg(dataSourceId);
				}
			}
		}
	}
	catch (NoSuchChartTemplateException nsct) {
	}
	
%>
<%
PortletURL selectDscfgUrl = renderResponse.createRenderURL();
selectDscfgUrl.setParameter("mvcPath", "/html/portlet/chart/datasourcecfg/select_dscfg.jsp");
selectDscfgUrl.setParameter("eventName", eventName);
selectDscfgUrl.setWindowState(new WindowState("pop_up"));
%>
<div class="control-group">
		<aui:input name="dataSourceCfgId" type="hidden" value="<%=option!=null?option.getDataSourceId():StringPool.BLANK %>"/>
		<aui:input inlineField="<%=true %>" value="<%=dscfg!=null?dscfg.getName():StringPool.BLANK %>" label="chart.baseconfig.select.dscfg" name="dataSourceCfgNameDisplay" id="dataSourceCfgNameDisplay" required="<%= true %>" disabled="true"  />
		<liferay-ui:icon
				cssClass="dscfg-selector"
				iconCssClass="icon-search"
				label="<%= true %>"
				linkCssClass="btn"
				message="select"
				url="javascript:;" 
			/>
</div>
<aui:model-context bean="<%=option%>" model="<%=Option.class%>" />
<aui:input name="dataSql" type="textarea" required="<%=true %>" label="chart.baseconfig.data.sql" style='<%= "height: 400px; width: 100%;" %>' />
<aui:script use="aui-base,escape,liferay-search-container,aui-ace-editor">
	A.getBody().delegate(
		'click',
		function(event) {
			event.preventDefault();

			Liferay.Util.selectEntity(
				{
					dialog: {
						constrain: true,
						modal: true,
						destroyOnHide: true
					},
					eventName: '<%= eventName %>',
					id: '<%= eventName %>' + event.currentTarget.attr('id'),
					title: '<%=LanguageUtil.get(pageContext, "chart.baseconfig.select.dscfg") %>',
					uri: '<%=selectDscfgUrl %>'
				},
				function(event) {
					//var name = A.Lang.String.unescapeEntities(event.name);
					A.one('#<portlet:namespace />dataSourceCfgId').val(event.datasourceid);
					A.one('#<portlet:namespace />dataSourceCfgNameDisplay').val(event.name);
					
				}
			);
		},
		'.dscfg-selector a'
	);
</aui:script>
